package Shell;

/**
 * Created with IntelliJ IDEA.
 * Description:希尔排序
 * User: 均先生
 * Date: 2023-11-18
 * Time: 19:44
 */
public class Shell {

    /**
     * 时间复杂度:O(N^1.3~1.5)
     * 稳定性:不稳定
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;//将每次分的组取半
            shell(array,gap);
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;//gap是分组后面一组的起始位置,j则是所分第一组的起始位置
            for (; j >= 0; j-=gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
}
